package de.baumann.browser.present

import de.baumann.browser.OdinApplication
import de.baumann.browser.R
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.AccountInfo
import de.baumann.browser.api.net.vo.Role
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IMineView.Companion.ACCOUNT_LOCK
import de.baumann.browser.iview.IMineView.Companion.SUPER_NODE_USER
import de.baumann.browser.iview.IWalletView
import de.baumann.browser.model.CardModel
import de.baumann.browser.model.NodeModel
import de.baumann.browser.model.UserModel
import de.baumann.browser.utils.getUser

class WalletPresenter : BasePresenter<IWalletView>() {

    private val userModel = UserModel()

    fun getUserBasicInfo() {
        val loginInfo = getUser()
        if (loginInfo == null && view != null) {
            view.setHeadhUrl("")
            view.setLoginName(OdinApplication.getInstance().getString(R.string.login_now))
            return
        }

        if (loginInfo != null) {
            view.setHeadhUrl(loginInfo.headUrl)

            val name = if (loginInfo.user_name.isEmpty()) {
                loginInfo.loginName
            } else {
                loginInfo.user_name
            }

            view.setLoginName(name)
            view.setPoolName(loginInfo.poolname)
            if (loginInfo.licence != null) {
                view.setLicenceStr(loginInfo.licence)
            }
        }
    }

    fun getWalletData() {
        this.userModel.odinData
                .subscribe(object : OdinObserver<AccountInfo>(view) {
                    override fun onSuccess(data: AccountInfo?) {
                        if (view != null) {
                            view.setOdinOver(data!!.profit)
                            view.setOdinFreeze(data.frozenProfit)
                        }
                    }
                })
    }

    fun isCanWihdraw() {
        CardModel().getUserRole()
                .subscribe(object : OdinObserver<Role>(view) {
                    override fun onSuccess(data: Role) {
                        view.let {
                            when (data.type) {
                                SUPER_NODE_USER -> view.isCanWithdraw(false)
                                else -> view.isCanWithdraw(true)
                            }
                        }
                    }
                })
    }
}